const Controller = require('../controller/user')

async function verifyAuthToken(req, res, next) {
    const { username, authToken } = req.cookies;
    // 判空
    if (!username || !authToken) {
        return returnVerifyFailed(res);
    }
    // 验证token
    try {
        const result = await Controller.verifyToken(username, authToken);
        if (result) {
            next();
        } else {
            returnVerifyFailed(res);
        }
    } catch (err) {
        console.error(err);
        returnVerifyFailed(res);
    }
}

function returnVerifyFailed(res) {
    res.json({
        status: 501,
        message: 'Login Verification Failed'
    })

}
module.exports = verifyAuthToken;